<?php
/**
 * Created by jinqiang on 2024/12/2 10:13.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Callback;


use App\Http\Models\Mysql\SsyAllocationOrderReturnTable;
use App\Http\Models\Mysql\SsyProcessingListTable;
use App\Http\Repository\KingdeeRepository;
use App\Http\Repository\WmsRepository;

class WmsService
{
    public function processing($param)
    {
        if (!($param['f_status_user'] ?? "" && $param['jw_number'] ?? "" && $param['processing_code'] ?? "")){
            return ['code' => -1, 'msg' => '缺失重要参数', 'data' => $param];
        }
        return (new SsyProcessingListTable())->updateDataWhere(['processing_code' => $param['processing_code']], [
            "jw_number" => $param['jw_number'],
            "f_status" => 1,
            "f_status_user" => $param['f_status_user'],
            "content_remarks" => "后台手动完结",
            "f_status_time" => date("Y-m-d H:i:s")
        ]);
    }

    public function allocation($param)
    {
        if (!($param['jw_allocation_number'] ?? "" && $param['order_type'] ?? "" && $param['goodsOwner'] ?? "" && $param['skudateil'] ?? []
            && count($param['skudateil']))){
            return ['code' => -1, 'msg' => '缺失重要参数', 'data' => $param];
        }
        $exist = (new SsyAllocationOrderReturnTable())->where('jw_allocation_number', $param['jw_allocation_number'])->exists();
        if($exist){
            return ['code' => -1, 'msg' => '重复回写'];
        }
        $isH = false;//是否归还单据
        $allocationNumbers = [];
        foreach ($param['skudateil'] as $item) {
            if(preg_match("/^(GH|SZH)/is", $item['allocation_number'])){
                $isH = true;
            }
            $data = [
                "allocation_number" => $item['allocation_number'],
                "transfer_type" => $param['transfer_type'],
                "sku_code" => $item['sku_code'],
                "sj_num" => $item['sj_num'],
                "db_num" => $item['db_num'],
                "jw_allocation_number" => $param['jw_allocation_number'],
                "order_type" => $param['order_type'],
                "goods_owner" => $param['goodsOwner'],
                "from_whcode" => $param['from_whcode'] ?? "",
                "target_whcode" => $param['target_whcode'] ?? "",
                "courier_number" => $param['courier_number'] ?? "",
                "snlist" => isset($item['snlist']) ? json_encode($item['snlist'], JSON_UNESCAPED_UNICODE) : "",
                "operator" => "SYSTEM",
                "createtime" => date("Y-m-d H:i:s")
            ];
            $allocationNumbers[$item['allocation_number']] = $param['courier_number'] ?? "";
            (new SsyAllocationOrderReturnTable())->createData($data);
        }
        //实际归还业务 按波次推送金蝶
        if($isH && $param['goodsOwner'] == "SSY" && $param['transfer_type'] == 1){
            $postData = ['timeout' => 1, 'json' => ['data' => json_encode($param, JSON_UNESCAPED_UNICODE)]];
            httpCurl(config("app.url")."/api/ext/kingdee/pushAllocationH", $postData);
        }
        //出库发送飞书消息
        if($param['transfer_type'] == 2){
            foreach ($allocationNumbers as $allocationNumber => $courierNumber){
                (new FeiShuService())->pushAllocationMsg($allocationNumber, $courierNumber);
            }
        }
        return ['code' => 0, 'msg' => '回调数据'];
    }
}
